//
//  RTJPrepaidRecordsController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/14.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJPrepaidRecordsController.h"
#import "RTJCapitalSubsidiaryCell.h"

@interface RTJPrepaidRecordsController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *data;
@property(nonatomic, assign) NSInteger currentPage;
@property (nonatomic, strong) NSMutableArray *selectedData;
@property (nonatomic, strong) NSMutableArray *indexArr;
@end

@implementation RTJPrepaidRecordsController

-(NSMutableArray *)data {
    
    if (!_data) {
        _data = [NSMutableArray array];
    }
    
    return _data;
}

-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"充值记录";
    
    self.tableView.tableFooterView=[UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedRowHeight=300;
    
    _selectedData  = [NSMutableArray array];
    _indexArr = [NSMutableArray array];
    
    self.currentPage = 1;
    self.tableView.mj_header = [CustomRefreshGifHeader headerWithRefreshingBlock:^{
        
        self.currentPage = 1;
        [self loadData];
        
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        self.currentPage ++;
        [self loadData];
        
    }];
    
    [self.tableView.mj_header beginRefreshing];

}

-(void)loadData{
    
    [SVProgressHUD  showWithStatus:nil];
    
    if (self.data.count == 0) {
        
        [self.view showEmptyViewWithStr:@"" image:nil callback:nil];
        
    }
    
    [RTJNetWork asyncRequestWithURL:@"user/recharge_log_list" params:@{@"page":@(_currentPage)} method:GET zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        [self.view removeEmptyView];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                NSArray *arr = [NSArray array];
                arr = data[@"data"][@"list"];
                
                if (arr.count > 0) {
                    
                    if (self.currentPage == 1) {
                        
                        [self.data removeAllObjects];
                    }
                    
                    [self.data addObjectsFromArray:arr];
                    
                    if (arr.count < ZXPAGE_SIZE) {
                        
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    }
                    
                }else{
                    
                    if (self.currentPage == 1) {
                        
                        
                        [self.data removeAllObjects];
                        
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                        [self.view showEmptyViewWithStr:@"暂无相关数据" image:[UIImage imageNamed:@"kbylogo"] callback:^{
                            [self loadData];
                        }];
                        
                    }else{
                        
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                        
                    }
                }
                
                [self.tableView reloadData];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                
                [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                    [self loadData];
                }];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
            
            [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                [self loadData];
            }];
            
        }
        
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  self.data.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewAutomaticDimension;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ID = @"RTJCapitalSubsidiaryCell";
    
    RTJCapitalSubsidiaryCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    
    if (!cell) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:@"RTJCapitalSubsidiaryCell" owner:self options:nil][2];
    }
    NSString *index = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    
    if (![self.indexArr containsObject:index]) {
        
        cell.isOpen = YES;
        
    }else{
        
        for (NSDictionary *dic in self.selectedData) {
            
            if([[dic allKeys] containsObject:index]) {
                
                NSString *isOpen = [NSString stringWithFormat:@"%@",[dic valueForKey:index]];
                
                if ([isOpen isEqualToString:@"1"]) {
                    
                    cell.isOpen = NO;
                    
                }else{
                    
                    cell.isOpen = YES;
                    
                }
                
            }
        }
        
    }
    
    cell.chongzhidata = _data[indexPath.row];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    return  cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.selectedData.count == 0) {
        
        NSString *index = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
        NSString *str = @"1";
        NSDictionary *dic = @{index:str};
        [self.selectedData addObject:dic];
        [self.indexArr addObject:index];
        
        NSIndexPath *indexPaths=[NSIndexPath indexPathForRow:indexPath.row inSection:0];
        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPaths,nil] withRowAnimation:UITableViewRowAnimationNone];
        
    }else{
        
        NSString *index = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
        
        if (![self.indexArr containsObject:index]) {
            
            [self.indexArr addObject:index];
            
            NSString *str = @"1";
            NSDictionary *dic = @{index:str};
            [self.selectedData addObject:dic];
            
            NSIndexPath *indexPaths=[NSIndexPath indexPathForRow:indexPath.row inSection:0];
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPaths,nil] withRowAnimation:UITableViewRowAnimationNone];
            
        }else{
            
            [self.selectedData enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                
                if ([[obj allKeys] containsObject:index]) {
                    
                    NSString *isOpen1 = [NSString stringWithFormat:@"%@",[obj valueForKey:index]];
                    
                    if ([isOpen1 isEqualToString:@"0"]) {
                        
                        NSDictionary *dic = @{index:@"1"};
                        [self.selectedData replaceObjectAtIndex:idx withObject:dic];
                        
                        NSIndexPath *indexPaths=[NSIndexPath indexPathForRow:indexPath.row inSection:0];
                        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPaths,nil] withRowAnimation:UITableViewRowAnimationNone];
                        
                    }else{
                        
                        NSDictionary *dic = @{index:@"0"};
                        [self.selectedData replaceObjectAtIndex:idx withObject:dic];
                        
                        NSIndexPath *indexPaths=[NSIndexPath indexPathForRow:indexPath.row inSection:0];
                        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPaths,nil] withRowAnimation:UITableViewRowAnimationNone];
                        
                    }
                    
                }
                
            }];
        }
    }
}

@end
